function getOrgProducts(orgListOrOrg) {
  const productsList = [];

  if (!Array.isArray(orgListOrOrg)) {
    orgListOrOrg = [orgListOrOrg];
  }

  orgListOrOrg.forEach(({ groupInfos }) => {
    groupInfos.forEach(({ products }) => {
      productsList.push(...products);
    });
  });

  const cartProductIds = productsList.map(({ id }) => {
    return id;
  });

  return {
    products: productsList,
    ids: cartProductIds,
  };
}

function getValidSelected(orgListOrOrg, selectedIds) {
  const { products, ids } = getOrgProducts(orgListOrOrg);

  const validProducts = products.filter((v) => {
    if (selectedIds !== undefined && selectedIds?.length > 0) {
      return selectedIds.includes(v.id);
    } else {
      return v.selected;
    }
  });
  const validIds = validProducts.map(({ id }) => {
    return id;
  });

  return {
    // 所有商品
    allProducts: products,
    allIds: ids,

    // 筛选后合法的商品
    validProducts: validProducts,
    validIds: validIds,
  };
}

export { getOrgProducts, getValidSelected };
